<template>
  <base-load-page :loadState="false" class="base-load-page">
    <template #head>
      <div style="padding-bottom: 15px" class="head-box">
        <div class="left-box">
          <base-date-picker
            v-model="dataRange"
            valueFormat="YYYY-MM-DD"
            format="YYYY-MM-DD"
            type="daterange"
            startPlaceholder="开始日期"
            endPlaceholder="结束日期"
            style="flex-grow: 0; width: 350px"
            clearable
            @change="getAxiosData(true)"
          ></base-date-picker>
          <base-input-search
            v-model="filterData.userId"
            clearable
            placeholder="请输入用户ID"
            style="width: 230px"
            :prefixIcon="IconSearch"
            @submit="getAxiosData(true)"
          ></base-input-search>
          <base-input-search
            v-model="filterData.openId"
            clearable
            placeholder="请输入用户openID"
            style="width: 230px"
            :prefixIcon="IconSearch"
            @submit="getAxiosData(true)"
          ></base-input-search>
          <base-input-search
            v-model="filterData.dramaTitle"
            clearable
            placeholder="请输入短剧名称"
            style="width: 230px"
            :prefixIcon="IconSearch"
            @submit="getAxiosData(true)"
          ></base-input-search>
          <bubble-select-local
            v-model="filterData.platformType"
            labelKey="label"
            valueKey="value"
            :filterable="false"
            style="width: 230px"
            :list="[
              { label: '全部', value: undefined },
              { label: '抖音', value: 'mp-toutiao' },
              { label: '快手', value: 'mp-kuaishou' },
              { label: '微信', value: 'mp-weixin' },
            ]"
            placeholder="请选择消费平台"
            @change="getAxiosData(true)"
          ></bubble-select-local>
        </div>
      </div>
    </template>
    <el-table ref="elTableRef" v-loading="baseData.loading" border height="100%" :data="tableData" showOverflowTooltip stripe style="width: 100%">
      <template #empty>
        <base-empty :description="description" :pageLoadButton="[{ text: '重新加载', callBack: () => getAxiosData() }]"></base-empty>
      </template>
      <el-table-column
        fixed
        type="index"
        :index="(index: number) => (filterData.current - 1) * filterData.size + index + 1"
        label="编号"
        sortable
        width="60"
        align="center"
        headerAlign="center"
      ></el-table-column>
      <el-table-column label="时间" prop="createTime" minWidth="180"></el-table-column>
      <el-table-column label="用户ID" prop="userId" minWidth="180"></el-table-column>
      <el-table-column label="openID" prop="openId" minWidth="340"></el-table-column>
      <el-table-column label="短剧名称" prop="dramaTitle" minWidth="180"></el-table-column>
      <el-table-column label="解锁平台" prop="platformTypeDesc" minWidth="100"></el-table-column>
      <el-table-column label="解锁集数id" prop="episodeId" minWidth="140"></el-table-column>
      <!-- <el-table-column label="开始解锁集数" prop="startUnlockEpisodeSeq" minWidth="110"></el-table-column> -->
      <el-table-column label="集数" prop="episodeSeq" minWidth="100"></el-table-column>
    </el-table>
    <template #foot>
      <div class="pagination-box">
        <el-pagination
          v-model:currentPage="filterData.current"
          v-model:pageSize="filterData.size"
          :pageSizes="[20, 30, 40, 50]"
          :disabled="baseData.loading"
          :pagerCount="13"
          layout="total, prev, pager, next, sizes, jumper"
          :total="baseData.total"
          @change="getAxiosData(true)"
        ></el-pagination>
      </div>
    </template>
  </base-load-page>
</template>

<script setup lang="ts">
  import IconSearch from '~icons/ep/search';
  import { type AppDramaUnlockRecord, dramaViewHistoryRecord } from '@/api/app-user-management';
  const { proxy } = getCurrentInstance() as any;
  const elTableRef: $RefComp<'ElTable'> = ref(null);
  const baseData = shallowReactive({
    type: 'add' as 'add' | 'edit',
    /** 加载状态 */
    loading: true,
    /** 总数量 */
    total: 0,
  });
  const dataRange = ref<[string, string] | undefined>();

  const filterData: Ref<AppDramaUnlockRecord> = ref<AppDramaUnlockRecord>({
    platformType: undefined,
    current: 1,
    size: 40,
    startDate: '',
    endDate: '',
    userId: '',
    dramaTitle: '',
  });

  const tableData = ref<any>();
  const description = ref('');
  onMounted(() => {
    dataRange.value = [proxy.$dayjs().format('YYYY-MM-DD'), proxy.$dayjs().format('YYYY-MM-DD')];
    getAxiosData();
  });

  /**
   * 设置表格数据
   */
  async function getAxiosData(showLoading = true) {
    if (Array.isArray(dataRange.value) && dataRange.value.length) {
      filterData.value.startDate = `${dataRange.value[0]} 00:00:00`;
      filterData.value.endDate = `${dataRange.value[1]} 23:59:59`;
    } else {
      filterData.value.startDate = '';
      filterData.value.endDate = '';
    }
    try {
      await nextTick();
      baseData.loading = Boolean(showLoading);
      const res = await dramaViewHistoryRecord(filterData.value);
      tableData.value = res.data.records;
      description.value = '暂无数据';
      filterData.value.current = res.data.current;
      filterData.value.size = res.data.size;
      baseData.total = res.data.total;
      baseData.loading = false;
    } catch (error) {
      baseData.loading = false;
      tableData.value = [];
      description.value = error.msg || '加载失败，-BD001';
    }
  }
</script>

<style lang="scss" scoped>
  .pagination-box {
    @include flex(row, nowrap, center, center, center);
    padding-top: 15px;
  }

  .head-box {
    @include flex(row, nowrap, flex-start, center, center);

    .left-box {
      // display: grid;
      display: flex;
      flex: 1;
      flex-wrap: wrap;
      // grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
      // grid-gap: 15px;
      gap: 14px;
      margin-right: 15px;
    }
  }
</style>
